import {defineStore} from "pinia";
import myAxios from "../http/myAxios";
import {responseDataHttp} from "../type/response";
import {UserInfo} from "../type/user";
import {showSuccessToast} from "vant";

// 用户 仓库
export const useUserStore = defineStore('user', () => {

    // 修改用户信息
    async function editUserInfo(name: string, sex: number, individuality: string) {
        const result = await myAxios<responseDataHttp<{ user: UserInfo }>>("/auth/user/update", {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            data: JSON.stringify({
                "name": name,
                "sex": sex,
                "individuality": individuality
            })
        }).then(r => r.data)

        showSuccessToast(result.msg);
        // console.log(result)
    }

    //修改用户头像

    // file是自带的文件类型
    async function imgUserInfo(avatar: File) {
        const data = new FormData()
        data.append("avatar", avatar)

        const result = await myAxios<responseDataHttp<any>>("/auth/user/updateAvatar", {
            method: "POST",
            data: data
        })

        console.log(result)
    }


    return {
        editUserInfo,
        imgUserInfo
    }
})